package com.ftg.learn.rabbitmq.simply;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

/**
 * 消费者
 *
 * @author 布衣
 */
public class Consumer {

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare("q01", false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        //消费 - 回调
        DeliverCallback a = (consumerTag, delivery) -> {
//            delivery bytes[]
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [x] Received '" + message + "'");
        };
        //消费q01队列
        channel.basicConsume("q01", true, a, consumerTag -> { });
    }


}
